//
//  homeViewController.m
//  
//
//  Created by turing on 15/11/24.
//
//

#import "homeViewController.h"

#import <BaiduMapAPI_Location/BMKLocationService.h>
#import <BaiduMapAPI_Map/BMKMapView.h>
#import <BaiduMapAPI_Map/BMKPointAnnotation.h>
#import "MineViewController.h"
#import "DataController.h"
#import "User.h"
#import "RegisterViewController.h"

@interface homeViewController ()<BMKMapViewDelegate,BMKLocationServiceDelegate>

@property(strong,nonatomic) BMKMapView         *mapView;     //地图视图
@property(strong,nonatomic) BMKLocationService *locService;  //定位服务
@property(strong,nonatomic) User               *user;        //用户信息

@end

@implementation homeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    //取得用户信息
    self.user = (User *)[NSKeyedUnarchiver unarchiveObjectWithFile:kUserPath];
    
    if (!self.user) {
        RegisterViewController *registerVC = [[RegisterViewController alloc] init];
        [self presentViewController:registerVC animated:YES completion:^{
            
        }];
    }
    
    self.navtitle.text = @"         首页";
    self.leftButton.hidden = YES;
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setBackgroundImage:[UIImage imageNamed:@"gr"]
                      forState:UIControlStateNormal];
    [button addTarget:self action:@selector(GotoSettings:)
     forControlEvents:UIControlEventTouchUpInside];
    button.frame = CGRectMake(self.view.frame.size.width - 39, 30, 25, 25);
    UIBarButtonItem *menuButton = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.rightBarButtonItem = menuButton;
    
    /*
    //APP安装目录中的document目录路径
    NSFileManager*fileManager =[NSFileManager defaultManager];
    NSError*error;
    NSArray*paths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
    NSString*documentsDirectory =[paths objectAtIndex:0];
    
    //APP安装目录中的document目录路径
    NSString*dbPath =[documentsDirectory stringByAppendingPathComponent:@"erea.db"];
    NSLog(@"沙盒路径==%@", dbPath);
     */
    //[self initWithMap];
}

- (void)GotoSettings:(UIButton *)button{
    MineViewController *mineVC = [[MineViewController alloc] initWithUser:self.user];
    [self.navigationController pushViewController:mineVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//  初始化地图
-(void)initWithMap
{
    //self.navtitle.text = @"首页";
    //self.leftButton.hidden = YES;
    [self.rightButton setImage:[UIImage imageNamed:@"gr"] forState:UIControlStateNormal];
    [self.rightButton setImage:[UIImage imageNamed:@"gr_pre"] forState:UIControlStateHighlighted];
    [self setRightButtonAction:@selector(logintAction)];

    //地图初始化
    _mapView = [[BMKMapView alloc]initWithFrame:CGRectMake(0, 0, kUIScreenWidth, kUIScreenHeight - [self.rdv_tabBarController tabBar].selectedItem.frame.size.height)];
    _mapView.showMapScaleBar = YES;
    [_mapView setZoomLevel:13];
    self.view = _mapView;
 
    //定位服务初始化
    _locService = [[BMKLocationService alloc]init];
}

//  登录 按钮
-(void)logintAction{
    
    //loginViewController *login = [[loginViewController alloc]init];
    //[self presentViewController:login animated:YES completion:^{
    
    //}];
}

//开始定位 普通定位态
-(void)startLocation
{
    NSLog(@"进入普通定位态");
    [_locService startUserLocationService];
    _mapView.showsUserLocation = NO;//先关闭显示的定位图层
    _mapView.userTrackingMode = BMKUserTrackingModeFollow;//设置定位的状态
    _mapView.showsUserLocation = YES;//显示定位图层
}

//停止定位
-(void)stopLocation{
    
    [_locService stopUserLocationService];
    _mapView.showsUserLocation = NO;
}


#pragma mark -
#pragma mark BMKMapViewDelegate

/**
 *地图初始化完毕时会调用此接口
 *@param mapview 地图View
 */

- (void)mapViewDidFinishLoading:(BMKMapView *)mapView
{
    
    // [self startLocation];
}

#pragma mark -
#pragma mark BMKLocationServiceDelegate
/**
 *在地图View将要启动定位时，会调用此函数
 *@param mapView 地图View
 */
- (void)willStartLocatingUser
{
    NSLog(@"start locate");
}

/**
 *用户位置更新后，会调用此函数
 *@param userLocation 新的用户位置
 */
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation
{
    //NSLog(@"didUpdateUserLocation lat %f,long %f",userLocation.location.coordinate.latitude,userLocation.location.coordinate.longitude);
    //[_mapView updateLocationData:userLocation];
    
    //设置定位位置为地图中心点
    CLLocationCoordinate2D pt = CLLocationCoordinate2DMake(_locService.userLocation.location.coordinate.latitude, _locService.userLocation.location.coordinate.longitude);
    [_mapView setCenterCoordinate:pt animated:YES];
    //添加地图大头针
    BMKPointAnnotation *pointAnnotation = [[BMKPointAnnotation alloc]init];
    pointAnnotation.coordinate = pt;
    pointAnnotation.title = @"我是大头针";
    [_mapView addAnnotation:pointAnnotation];
    [self stopLocation];
}

/**
 *在地图View停止定位后，会调用此函数
 *@param mapView 地图View
 */
- (void)didStopLocatingUser
{
    NSLog(@"stop locate");
}

/**
 *定位失败后，会调用此函数
 *@param mapView 地图View
 *@param error 错误号，参考CLError.h中定义的错误号
 */
- (void)didFailToLocateUserWithError:(NSError *)error
{
    NSLog(@"location error");
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
